//角色实例
let request = require('superagent')
require('superagent-proxy')(request)
var sdkHandler = require("./sdkHandler.js")
var targetUrl = "http://183.255.124.32:3011/"
var weights = {
	"checkpointsSuccess" : 2000,
	"gainRandChest" : 2000,
	"lordLvUp" : 2500,
	"getQuickOnhookAward" : 2000,
	"relogin" : 1000
}
var allWeight = 0
for(var i in weights)
	allWeight += weights[i]
var model = function(manage,id) {
	this.manage = manage
	this.id = id
	this.ip = ""
	this.port = ""
	this.proxyServer = ""
	this.proxyUrl = ""
	this.unionid = Date.now()
	this.liveTime = Math.ceil(3 + Math.random() * 27)
	this.endTime = Date.now() + this.liveTime * 60000
	this.nextExec = Date.now() + 3000
	this.playerInfo = {}
	this.cLv = 1
	this.init()
}
model.prototype.update = function() {
	if(Date.now() > this.nextExec)
		this.randExec()
	if(Date.now() > this.endTime)
		this.destroy()
}
//初始化
model.prototype.init = function() {
	//申请IP
	var self = this
	sdkHandler.applyIp(1,self.liveTime,function(data) {
		if(data && data.code == 200){
			self.ip = data.data[0].ip
			self.port = data.data[0].port
			self.proxyServer = self.ip+":"+self.port
			self.proxyUrl = 'http://' + self.proxyServer
			//申请创号
			self.createRole()
		}else{
			console.log("申请IP失败",data)
			self.destroy()
		}
	})
}
//执行操作
//创号
model.prototype.createRole = function() {
	var self = this
	self.request("playerLogin",{unionid:self.unionid},function(data) {
    	var info = JSON.parse(data)
        if(!info.flag){
        	self.destroy()
        	return
        }
        self.playerInfo = info.data
        console.log(self.playerInfo.name,self.playerInfo.uid,"活跃时间:"+self.liveTime+"min")
	})
}
//随机操作
model.prototype.randExec = function() {
	var self = this
	self.nextExec =  Date.now() + (2000 + Math.random() * 15000)
	var rand = Math.random() * allWeight
	for(var i in weights){
		if(rand < weights[i]){
			console.log("randExec ",i)
			self[i]()
			break
		}else{
			rand -= weights[i]
		}
	}
}
//挑战主线
model.prototype.checkpointsSuccess = function() {
	var self = this
	self.request("checkpointsSuccess",{uid:self.playerInfo.uid,level:self.cLv++},function(data) {
    	var info = JSON.parse(data)
        console.log(data)
	})
}
//获得随机奖励
model.prototype.gainRandChest = function() {
	var self = this
	self.request("gainRandChest",{uid:self.playerInfo.uid},function(data) {
    	var info = JSON.parse(data)
        console.log(data)
	})
}
//主公等级提升
model.prototype.lordLvUp = function() {
	var self = this
	self.request("lordLvUp",{uid:self.playerInfo.uid},function(data) {
    	var info = JSON.parse(data)
        console.log(data)
	})
}
//领取快速挂机奖励
model.prototype.getQuickOnhookAward = function() {
	var self = this
	self.request("getQuickOnhookAward",{uid:self.playerInfo.uid},function(data) {
    	var info = JSON.parse(data)
        console.log(data)
	})
}
//重登
model.prototype.relogin = function() {
	var self = this
	self.request("playerLeave",{accId:self.playerInfo.accId,uid:self.playerInfo.uid,name:encodeURI(self.playerInfo.name)},function(data) {
		self.request("playerLogin",{unionid:self.unionid},function(data) {
	    	var info = JSON.parse(data)
	        if(!info.flag){
	        	self.destroy()
	        	return
	        }
	        self.playerInfo = info.data
		})
	})
}
model.prototype.destroy = function() {
	//角色离线
	var self = this
	if(self.playerInfo.uid){
		self.request("playerLeave",{accId:self.playerInfo.accId,uid:self.playerInfo.uid,name:encodeURI(self.playerInfo.name)},function(data) {
	    	var info = JSON.parse(data)
	        if(!info.flag){
	        	self.destroy()
	        	return
	        }
	        self.playerInfo = info.data
		})
	}
	this.manage.destoryPlayer(this.id)
}
model.prototype.request = function(url,args,cb) {
	var self = this
	var url = targetUrl+url+"?"
	for(var i in args)
		url += i+"="+args[i]+"&"
	request.get(url)
    .proxy(self.proxyUrl)
    .then(res => {
        cb(res.text)
    }).catch(err => {
        console.log(err.message)
    })
}
module.exports = model